package com.wayne.security.controller;

import com.wayne.security.model.NoticeMessage;
import com.wayne.security.service.NoticeMessageService;
import org.springframework.security.acls.domain.BasePermission;
import org.springframework.security.acls.domain.ObjectIdentityImpl;
import org.springframework.security.acls.domain.PrincipalSid;
import org.springframework.security.acls.jdbc.JdbcMutableAclService;
import org.springframework.security.acls.model.MutableAcl;
import org.springframework.security.acls.model.ObjectIdentity;
import org.springframework.security.acls.model.Permission;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class NoticeMessageController {
  @Resource
  NoticeMessageService noticeMessageService;

  @GetMapping("/all")
  public List<NoticeMessage> findAll() {
    return noticeMessageService.findAll();
  }

  @GetMapping("/by/{id}")
  public NoticeMessage findById(@PathVariable Integer id) {
    return noticeMessageService.findById(id);
  }

  @PostMapping("/save")
  public NoticeMessage save(NoticeMessage noticeMessage) {
    return noticeMessageService.save(noticeMessage);
  }

  @Resource
  JdbcMutableAclService jdbcMutableAclService;

  @GetMapping("/test01")
  @Transactional
  public void test01() {
    ObjectIdentity objectIdentity = new ObjectIdentityImpl(NoticeMessage.class, 1);
    Permission p = BasePermission.ADMINISTRATION;
    MutableAcl acl = jdbcMutableAclService.createAcl(objectIdentity);
    acl.insertAce(acl.getEntries().size(), p, new PrincipalSid("hr"), true);
    jdbcMutableAclService.updateAcl(acl);
  }
}